﻿(function (root) {
    var DataContent = $("table > tbody");
    var request = $.ajax({
        url: "/Api/GetTemperature",
        method: "Get"
    });
    request.done(function (data) {
        data = JSON.parse(data);
        console.log(data);
        if (data.status === 0) {
            alert("服务器错误");
            return;
        }
        generatorHtml(parseData(data));
    });

    function parseData(data) {
        data = data.msg.list;
        return data.map(function (ele) {
            ele.deviceName = "intel";
            ele.deviceId = "1";
            ele.deviceLocation = "Dalian";
            var date = new Date(ele.createdAt);
            ele.date = date.getFullYear() + "年" + date.getMonth() + "月" + date.getDay() + "号" + "  " + date.getHours() + "点" + date.getMinutes() + "分";
            return ele;
        });
    }

    function generatorHtml(dataArray) {
        console.log(dataArray);
        for (var idx = 0; idx < dataArray.length; idx++) {
            DataContent.append("<tr><td>" + dataArray[idx].temperature + 
                "</td><td>" + dataArray[idx].deviceLocation + 
                "</td><td>" + dataArray[idx].date +
                "</td><td>" + dataArray[idx].deviceId +
                "</td><td>" + dataArray[idx].deviceName +
                "</td></tr>");
        }
    }
}(this));